﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace ElectronicCommerce.Framework.Data.Utility.LinqExtension
{
    public class DataContextWrapper
    {
        public DataContext DataContext { get; private set; }

        public DataContextWrapper(DataContext dataContext)
        {
            DataContext = dataContext;
        }

        /// <summary>
        /// 执行SQL语句，成功后填充到实体集合中
        /// </summary>
        /// <param name="query"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public IEnumerable<TResult> ExecuteQuery<TResult>(string query, params SqlParameter[] parameters)
        {
            object[] parametersInner = HandleSQL(ref query, parameters);
            return DataContext.ExecuteQuery<TResult>(query, parametersInner);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataSet ExecuteSP(string command, params SqlParameter[] parameters)
        {
            return ExecuteQuery(CommandType.StoredProcedure, command, parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataSet ExecuteQuery(string command, params SqlParameter[] parameters)
        {
            return ExecuteQuery(CommandType.Text, command, parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="commandType"></param>
        /// <param name="command"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataSet ExecuteQuery(CommandType commandType, string command, params SqlParameter[] parameters)
        {
            return SqlHelper.ExecuteDataset(DataContext.Connection.ConnectionString, commandType, command, parameters);
        }

        /// <summary>
        /// 执行sql命令，成功返回影响的记录数
        /// </summary>
        /// <param name="command"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public int ExecuteCommand(string command, params IDbDataParameter[] parameters)
        {
            object[] parametersInner = HandleSQL(ref command, parameters);
            return DataContext.ExecuteCommand(command, parametersInner);
        }

        /// <summary>
        /// 拼SQL From Common Parameters TO LINQ Parameters
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private object[] HandleSQL(ref string sql, IDbDataParameter[] parameters)
        {
            ArrayList paraArrayList = new ArrayList();

            if (parameters != null && parameters.Length > 0)
            {
                string paraName = string.Empty;
                for (int i = 0; i < parameters.Length; i++)
                {
                    paraName = parameters[i].ParameterName;
                    string innerParaName = paraName.StartsWith("@") ? paraName : "@" + paraName;
                    sql = sql.Replace(innerParaName, "{" + i + "}");
                    paraArrayList.Add(parameters[i].Value);
                }
            }

            return paraArrayList.ToArray();
        }
    }
}
